//
//  ViewController.m
//  UI_14_UICollectionView
//
//  Created by li cheng on 16/8/2.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1.UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.默认大小, 50 50
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    // 2.滑动方向,会改变
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 3.最小行间距
    layout.minimumLineSpacing = 5;
    // 4.最下列间距
    layout.minimumInteritemSpacing = 5;
    // 5.sectionIset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    // 6.区头大小,横向滚动时,设置宽;纵向滚动时,设置高
    layout.headerReferenceSize = CGSizeMake(100, 100);
    // 7.区尾大小
    layout.footerReferenceSize = CGSizeMake(200, 200);
    // 8.区头悬浮效果
    layout.sectionHeadersPinToVisibleBounds = YES;
    // 9.区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    // 2.UICollectionView的创建
    // 参数:
    // 1: frame
    // 2: layout(布局 )
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    [self.view addSubview:myCollectionView];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    // 注册重用池
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1"];
     [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"pool2"];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 3;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 23;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
        UICollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"pool1" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor yellowColor];
        return reuesableView;
    } else {
        UICollectionReusableView *reuesableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"pool2" forIndexPath:indexPath];
        reuesableView.backgroundColor = [UIColor purpleColor];
        return reuesableView;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
